.TH std::timespec_get 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::timespec_get \- std::timespec_get

.SH Synopsis
   Defined in header <ctime>
   int timespec_get( std::timespec* ts, int base ); \fB(1)\fP \fI(since C++17)\fP
   #define TIME_UTC /* implementation-defined */    \fB(2)\fP \fI(since C++17)\fP

   1) Modifies the std::timespec object pointed to by ts to hold the current calendar
   time in the time base base.
   2) Expands to a value suitable for use as the base argument of std::timespec_get.

   Other macro constants beginning with TIME_ may be provided by the implementation to
   indicate additional time bases.

   If base is TIME_UTC, then

     * ts->tv_sec is set to the number of seconds since an implementation defined
       epoch, truncated to a whole value,
     * ts->tv_nsec member is set to the integral number of nanoseconds, rounded to the
       resolution of the system clock.

.SH Parameters

   ts   - pointer to an object of type std::timespec
   base - TIME_UTC or another nonzero integer value indicating the time base

.SH Return value

   The value of base if successful, zero otherwise.

.SH Notes

   The POSIX function clock_gettime(CLOCK_REALTIME, ts) may also be used to populate a
   std::timespec with the time since the Epoch.

.SH Example


// Run this code

 #include <ctime>
 #include <iostream>

 int main()
 {
     std::timespec ts;
     std::timespec_get(&ts, TIME_UTC);
     char buf[100];
     std::strftime(buf, sizeof buf, "%D %T", std::gmtime(&ts.tv_sec));
     std::cout << "Current time: " << buf << '.' << ts.tv_nsec << " UTC\\n";
 }

.SH Possible output:

 Current time: 06/24/16 20:07:42.949494132 UTC

.SH See also

   timespec time in seconds and nanoseconds
   \fI(C++17)\fP  (struct)
   time     returns the current time of the system as time since epoch
            \fI(function)\fP
   C documentation for
   timespec_get
